/**
 * 路由测试脚本
 * 用于验证路由功能是否正常工作
 */

// 导入路由助手
const RouteHelper = require('../framework/core/route_helper.js');

// 测试用例
const testCases = [
  // 跨项目API测试
  { 
    route: 'oa_api/user_info',
    expected: {
      pid: 'oa',
      routePath: 'user_info',
      isSystemRoute: false,
      isCrossProjectApi: true
    }
  },
  { 
    route: 'plm_api/project_list',
    expected: {
      pid: 'plm',
      routePath: 'project_list',
      isSystemRoute: false,
      isCrossProjectApi: true
    }
  },
  
  // 系统路由测试
  {
    route: 'system/config',
    expected: {
      pid: 'system',
      routePath: 'system/config',
      isSystemRoute: true,
      isCrossProjectApi: false
    }
  },
  
  // PLM项目路由测试
  {
    route: 'project/list',
    expected: {
      pid: 'plm',
      routePath: 'project/list',
      isSystemRoute: false,
      isCrossProjectApi: false
    }
  },
  {
    route: 'task/detail',
    expected: {
      pid: 'plm',
      routePath: 'task/detail',
      isSystemRoute: false,
      isCrossProjectApi: false
    }
  },
  
  // OA项目路由测试
  {
    route: 'flow/list',
    expected: {
      pid: 'oa',
      routePath: 'flow/list',
      isSystemRoute: false,
      isCrossProjectApi: false
    }
  }
];

// 执行测试
console.log('====== 路由解析测试 ======');
console.log('');

let passCount = 0;
let failCount = 0;

testCases.forEach((testCase, index) => {
  const { route, expected } = testCase;
  
  // 获取路由解析结果
  const actual = RouteHelper.parseRoute(route);
  
  // 测试结果
  const isPass = (
    actual.pid === expected.pid &&
    actual.routePath === expected.routePath &&
    actual.isSystemRoute === expected.isSystemRoute &&
    actual.isCrossProjectApi === expected.isCrossProjectApi
  );
  
  if (isPass) {
    passCount++;
    console.log(`✅ 测试 #${index + 1} 通过`);
  } else {
    failCount++;
    console.log(`❌ 测试 #${index + 1} 失败`);
  }
  
  console.log(`   路由: ${route}`);
  console.log(`   期望: ${JSON.stringify(expected)}`);
  console.log(`   实际: ${JSON.stringify(actual)}`);
  console.log('');
});

// 测试结果汇总
console.log('====== 测试结果汇总 ======');
console.log(`总测试: ${testCases.length}`);
console.log(`通过: ${passCount}`);
console.log(`失败: ${failCount}`);
console.log(`通过率: ${(passCount / testCases.length * 100).toFixed(2)}%`);

// 完成测试
console.log('');
console.log('路由测试完成！');

// 如果有测试失败，退出码为1
if (failCount > 0) {
  process.exit(1);
} 